﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Vk.Common
{
	public class Tools
	{
		/// <summary>
		/// find the frist child object of type T by its dataContext
		/// </summary>
		/// <param name="parent"></param>
		/// <param name="dataContext"></param>
		/// <returns></returns>
		public static T FindChildRecursive<T>(DependencyObject parent, object dataContext)
			where T : FrameworkElement
		{
			DependencyObject ret = null;

			int count = VisualTreeHelper.GetChildrenCount(parent);
			if (count > 0)
			{
				for (int i = 0; i < count; i++)
				{
					DependencyObject child = VisualTreeHelper.GetChild(parent, i);
					if ((child is T) && (child as FrameworkElement).DataContext == dataContext)
					{
						ret = child;
						break;
					}

					ret = FindChildRecursive<T>(child, dataContext);
					if (ret != null)
					{
						break;
					}
				}
			}

			return ret as T;
		}
	}
}
